package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.PwdStrategy;
import com.ccp.dev.system.service.PwdStrategyService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 密码策略 控制器类
 * @author y
 */
@Controller
@Action(ownermodel = SysAuditModelType.SYSTEM_SETTING)
@RequestMapping(UrlConstants.PROJECT_ROOT+UrlConstants.PROJECT_MODULE_SYSTEM+"/pwdStrategy")
public class PwdStrategyController extends SysBaseController {
	@Resource
	private PwdStrategyService pwdStrategyService;

	/**
	 * 添加或更新密码策略。
	 */
	@Action(description = "添加或更新密码策略",
			detail = "<#if entity.id??>更新策略【${entity.desc}】<#else>添加策略【${entity.desc}】</#if><#if success>成功<#else>失败</#if>",
			exectype = "编辑日志")
	@ResponseBody
	@PostMapping(UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
	public ResultData save(@RequestBody PwdStrategy pwdStrategy) {
		SysAuditThreadLocalHolder.putParamerter("entity", pwdStrategy);
		try {
			pwdStrategyService.save(pwdStrategy);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("save.success");
		} catch (Exception e) {
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataSuccess("save.failure");
		}
	}

	/**
	 * 取得密码策略分页列表
	 */
	@Action(description = "取得密码策略分页列表<#if success>成功<#else>失败</#if>",exectype = "查询日志")
	@ResponseBody
	@GetMapping(UrlConstants.MODULE_FUNCTION_LIST)
	public ResultData list(HttpServletRequest request){
		try{
			QueryFilter queryFilter = new QueryFilter(request);
			List<PwdStrategy> list = pwdStrategyService.queryList(queryFilter);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success",queryFilter.getPageBean().getTotalCount(),list);
		}catch (Exception e){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataSuccess("operation.failure");
		}
	}

	/**
	 * 删除密码策略
	 */
	@Action(description = "删除密码策略",
			detail = "删除密码策略<#if success>成功<#else>失败</#if>",
			exectype = "删除日志")
	@ResponseBody
	@PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
	public ResultData del(HttpServletRequest request){
		try {
			String[] lAryId = RequestUtil.getStringAryByStr(request, "id");
			pwdStrategyService.delByIdArray(lAryId);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("delete.success");
		} catch (Exception ex) {
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("delete.failure");
		}
	}
	/**
	 * 更加id获取model
	 */
	@Action(description = "获取密码策略详细信息<#if success>成功<#else>失败</#if>",exectype = "查询日志")
	@ResponseBody
	@GetMapping(UrlConstants.MODULE_FUNCTION_GET)
	public ResultData get(HttpServletRequest request){
		try {
			String id = RequestUtil.getString(request, "id");
			PwdStrategy pwdStrategy =  pwdStrategyService.getOneById(id);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success",0L,pwdStrategy);
		} catch (Exception ex) {
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataSuccess("operation.failure");
		}
	}

	/**
	 * 更新启动状态
	 * @param request  HttpServletRequest
	 * @return
	 */
	@Action(description = "更新密码策略启动状态",
			detail = "更新密码策略启动状态<#if success>成功<#else>失败</#if>",
			exectype = "编辑日志")
	@ResponseBody
	@GetMapping("/updateEnableState")
	public ResultData updateEnableState(HttpServletRequest request) {
		try {
			Short enable = RequestUtil.getShort(request, "enable");
			String id = RequestUtil.getString(request, "id");
			pwdStrategyService.updateEnable(id, enable);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success");
		} catch (Exception e) {
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataSuccess("operation.failure");
		}
	}


	/**
	 * 功能描述: 跳转到首页
	 *
	 * @return: java.lang.String
	 * @auther: y
	 * @date: 2019-09-17 10:41:28
	 */
	@RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
	@Action(description="密码策略列表页跳转",exectype = "页面跳转日志")
	public String list() {
		return "sysPwdStrategy/list";
	}

	/**
	 * 功能描述: 跳转到新增页面
	 *
	 * @return: java.lang.String
	 * @auther: y
	 * @date: 2019-09-17 10:41:28
	 */
	@RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
	@Action(description="密码策略新增页跳转",exectype = "页面跳转日志")
	public String add(HttpServletRequest request, ModelMap modelMap) {
		String id = RequestUtil.getString(request,"id");
		modelMap.put("id",id);
		return "sysPwdStrategy/add";
	}
}
